﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///ZHJBXX 的摘要说明
/// </summary>
public partial class ZHJBXX
{
    public enum statusAccount { 正常 = 1, 冻结 }
    public enum statusCard { 认证通过 = 1, 未认证, 认证中 }
    public static ZHJBXX GetByAddUserID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ZHJBXX.FirstOrDefault(row => row.AddUserID == id);
        }
    }

    public static List<ZHJBXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ZHJBXX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.ZHJBXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<ZHJBXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.ZHJBXX.FirstOrDefault(row => row.AddUserID == id);
            if (objItem == null)
                return;
            context.ZHJBXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            ZHJBXX objZHJBXX = ZHJBXX.GetByAddUserID(ID);
            if (objZHJBXX == null)
                return;
            objZHJBXX.Enable_Flag = 0;
            objZHJBXX.Update();
        }
    }

    public static List<ZHJBXX> GetAllByConditon(int userID, int status, string txtAccountNo, string txtYuEFrom, string txtYuETo, string txtDJJEFrom, string txtDJJETo, string AddTimeForm, string AddTimeTo, int cardStatus)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.ZHJBXX
                        where t.Enable_Flag == 1
                        select t;
            if (userID != 0)
                query = query.Where(row => row.AddUserID == userID);
            if (status != 0)
                query = query.Where(row => row.Status == status);
            if (!string.IsNullOrEmpty(txtAccountNo))
                query = query.Where(row => row.AccountNo.Contains(txtAccountNo));
            if (!string.IsNullOrEmpty(txtYuEFrom))
                query = query.Where(row => row.YuE >= DongDong.Common.Misc.ConvertToDecimal(txtYuEFrom));
            if (!string.IsNullOrEmpty(txtYuETo))
                query = query.Where(row => row.YuE <= DongDong.Common.Misc.ConvertToDecimal(txtYuETo));
            if (!string.IsNullOrEmpty(txtDJJEFrom))
                query = query.Where(row => row.DJJE >= DongDong.Common.Misc.ConvertToDecimal(txtDJJEFrom));
            if (!string.IsNullOrEmpty(txtDJJETo))
                query = query.Where(row => row.DJJE <= DongDong.Common.Misc.ConvertToDecimal(txtDJJETo));
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (cardStatus != 0)
                query = query.Where(row => row.CardStatus == cardStatus);
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }
}